﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using MySql.Data.MySqlClient;
using System.Data;

namespace VatTuDAO
{
    public class LoaiNguoiDungDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        //Lay mot loai nguoi dung 
        public LoaiNguoiDungDTO SelectLoaiNguoiDung(int maLoaiNguoiDung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiNguoiDung where MaLoaiNguoiDung=@MaloaiNguoiDung";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", maLoaiNguoiDung));
          
            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];
            if (row == null)
                return null;
            LoaiNguoiDungDTO dto = new LoaiNguoiDungDTO();
            dto.MaLoaiNguoiDung = int.Parse(row["MaLoaiNguoiDung"].ToString());
            dto.TenLoaiNguoiDung = row["TenLoaiNguoiDung"].ToString();
            dto.PhanTramUuDai = int.Parse(row["PhanTramUuDai"].ToString());
            return dto;
        }

        //  Lay danh sach loai nguoi dung 
        public List<LoaiNguoiDungDTO> SelectLoaiNguoiDung()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiNguoiDung";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            List<LoaiNguoiDungDTO> ds = new List<LoaiNguoiDungDTO>();
            LoaiNguoiDungDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new LoaiNguoiDungDTO();
                    dto.MaLoaiNguoiDung = int.Parse(row["MaLoaiNguoiDung"].ToString());
                    dto.TenLoaiNguoiDung = row["TenLoaiNguoiDung"].ToString();
                    dto.PhanTramUuDai = int.Parse(row["PhanTramUuDai"].ToString());


                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }

        public List<LoaiNguoiDungDTO> SelectLoaiNguoiDung(string tenLoaiNguoiDung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiNguoiDung where TenLoaiNguoiDung like '"+ tenLoaiNguoiDung+" %'";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            List<LoaiNguoiDungDTO> ds = new List<LoaiNguoiDungDTO>();
            LoaiNguoiDungDTO dto;

            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    dto = new LoaiNguoiDungDTO();
                    dto.MaLoaiNguoiDung = int.Parse(row["MaLoaiNguoiDung"].ToString());
                    dto.TenLoaiNguoiDung = row["TenLoaiNguoiDung"].ToString();
                    dto.PhanTramUuDai = int.Parse(row["PhanTramUuDai"].ToString());


                    ds.Add(dto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }

        public Boolean InsertNguoiDung(NguoiDungDTO NguoiDung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into NguoiDung(MaNguoiDung,TenNguoiDung,GioiTinh,CMND,DiaChi,DienThoai,Email,TenDangNhap,MatKhau,MaLoaiNguoiDung) values(@MaNguoiDung,@TenNguoiDung,@GioiTinh,@CMND,@DiaChi,@DienThoai,@Email,@TenDangNhap,@MatKhau,@MaLoaiNguoiDung)";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@MaNguoiDung", NguoiDung.MaLoaiNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@TenNguoiDung", NguoiDung.TenNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@GioiTinh", NguoiDung.GioiTinh));
            _cmd.Parameters.Add(new MySqlParameter("@CMND", NguoiDung.CMND));
            _cmd.Parameters.Add(new MySqlParameter("@DiaChi", NguoiDung.DiaChi));
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai", NguoiDung.DienThoai));
            _cmd.Parameters.Add(new MySqlParameter("@Email", NguoiDung.Email));
            _cmd.Parameters.Add(new MySqlParameter("@TenDangNhap", NguoiDung.TenDangNhap));
            _cmd.Parameters.Add(new MySqlParameter("@MatKhau", NguoiDung.MatKhau));
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", NguoiDung.MaLoaiNguoiDung));


            return ExecuteNonQuery(_cmd);
        }

        // update loai Nguoi dung 

        public Boolean UpdateLoaiNguoiDung(LoaiNguoiDungDTO loainguoidung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update LoaiNguoiDung set TenLoaiNguoiDung = @TenLoaiNguoiDung where MaLoaiNguoiDung=@MaLoaiNguoiDung";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@TenLoaiNguoiDung", loainguoidung.TenLoaiNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", loainguoidung.MaLoaiNguoiDung));

            return ExecuteNonQuery(_cmd);
        }


        //Delete 
        public Boolean DeleteLoaiNguoiDung(int MaLoaiNguoiDung)
        {

            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from LoaiNguoiDung";
            _cmd.CommandText += " where MaLoaiNguoiDung=@MaLoaiNguoiDung";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", MaLoaiNguoiDung));

            if (ExecuteNonQuery(_cmd) == false)
            {
                return false;
            }

            return true;
        }
       


    }
}
